/**
 * Created by bgy on 2016/12/13.
 */
import $ from '../sources/jquery-2.2.4';
import Global from '../actions/Global';
//截取地址栏的值

var url = 'http%3A%2F%2Fsunzone.wicp.net%2F%3F%26connect_redirect%3D1%23wechat_redirect%26wxOrgID%3Dsz-baiguoyuan%26openid%3DowJnujrC0pLesLLimVF17-IwbG8U%26access_token%3DpUoW67Q9qEZmEt_A5YMLLH5EOhWmDUFfk-03xze2YOdX-A2LTTPhLWU2_RMDI6Rtpr8qRo618oIvPxxxW73zLa51FuLM7S-gZK5HvF6xo74&version=16050223&devicetype=iOS10.1.1&lang=zh_CN&scene=1&exportkey=AXXgBrqYV%2F69vQ9qwWoSiOs%3D&pass_ticket=uE%2FD%2Bvck88B%2For8iao0aGGedOwJxuWNjripWvjjiQzITVkZYJcJNo9C9oPwAJxz%2B&wechat_real_lang=zh_CN';
var urlString = encodeURIComponent(url);

function GetRequest() {
  var url = location.search; //获取url中'?'符后的字串
  var theRequest = new Object();
  if (url.indexOf('?') != -1) {
    var str = url.substr(1),
      strs = str.split('&');
    for(var i = 0; i < strs.length; i ++) {
      theRequest[strs[i].split('=')[0]]=(strs[i].split('=')[1]);
    }
  }
  return theRequest;
}
var openId = GetRequest().openId ? GetRequest().openId:'onx3Gs5cnT81Rfvi0YiWsadffasdf1';
window.localStorage.setItem('openId', openId);
var login = {
  isLogin: function () {
    var vendorInfo = window.localStorage.getItem('vendorInfo');
    if (vendorInfo) {
      return true;
    } else {
      //判断是否绑定微信，绑定则自动登录，返会true，否则返回false；
      $.ajax({
        type: 'post',
        url: Global.host + '/account/wxBlindState',
        data: JSON.stringify({
          "openId": openId
        }),
        async: false,
        dataType: 'json',
        contentType: 'application/json;charset=utf-8',
        success: function (data) {
          if (data.errorCode == 0 && data.isBind == 'Y') {
            window.localStorage.setItem('vendorInfo',JSON.stringify(data.data.VendorInfo));
            return true;
          } else {
            return false;
          }
        },
        error: function () {
          return false;
        }
      });
    }
  }
}
export default login;
